package com.huawei.chengdu.meetingdemo.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.meeting.CallRecord.model.CallRecordInfo;
import com.device.idea.meeting.CallRecord.model.CallRecordType;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.CallTimeUtils;

import java.util.ArrayList;
import java.util.List;

public class CallRecordListAdapter extends RecyclerView.Adapter<CallRecordListAdapter.CallRecordViewHolder> {
    private final List<CallRecordInfo> callRecordList = new ArrayList<>();

    public CallRecordListAdapter() {
    }

    @NonNull
    @Override
    public CallRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_recent_call, parent, false);
        return new CallRecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CallRecordViewHolder holder, int position) {
        CallRecordInfo callRecordInfo = callRecordList.get(position);
        holder.recordName.setText(callRecordInfo.getCallName().trim());
        holder.recordCallTime.setText(CallTimeUtils.callTimeToString(callRecordInfo.getStartTime()));
        int duration = CallTimeUtils.callTimeToSecond(callRecordInfo.getEndTime()) -
                CallTimeUtils.callTimeToSecond(callRecordInfo.getStartTime());
        String recordTypeStr = "";
        if (callRecordInfo.getRecordType() == CallRecordType.RECORD_OUT) {
            recordTypeStr = "呼出 " + duration + "秒";
            holder.recordTypePic.setImageResource(R.drawable.call_out);
        }
        if (callRecordInfo.getRecordType() == CallRecordType.RECORD_ACCEPT) {
            recordTypeStr = "呼入 " + duration + "秒";
            holder.recordTypePic.setImageResource(R.drawable.call_out);
        }
        if (callRecordInfo.getRecordType() == CallRecordType.RECORD_NOANS) {
            recordTypeStr = "未接听";
            holder.recordTypePic.setImageResource(R.drawable.miss_call);
        }
        holder.recordType.setText(recordTypeStr);
        holder.recordCall.setOnClickListener((v) -> ConfSdkPresenter.callRecordCall(callRecordInfo));
        holder.recordDelete.setOnClickListener((v) -> ConfSdkPresenter.deleteCallRecord(callRecordInfo.getRecordId(),
                callRecordInfo.getRecordType()));
    }

    public void updateCallRecordList(List<CallRecordInfo> newCallRecordList) {
        callRecordList.clear();
        callRecordList.addAll(newCallRecordList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return callRecordList.size();
    }

    public static class CallRecordViewHolder extends RecyclerView.ViewHolder {
        private final TextView recordName;
        private final TextView recordType;
        private final TextView recordCallTime;
        private final ImageView recordTypePic;
        private final View recordCall;
        private final View recordDelete;


        public CallRecordViewHolder(@NonNull View itemView) {
            super(itemView);
            recordName = itemView.findViewById(R.id.record_name);
            recordType = itemView.findViewById(R.id.call_record_type);
            recordCallTime = itemView.findViewById(R.id.record_call_time);
            recordTypePic = itemView.findViewById(R.id.call_record_type_pic);
            recordCall = itemView.findViewById(R.id.recent_call);
            recordDelete = itemView.findViewById(R.id.recent_delete);
        }
    }

}
